package org.apache.lucene.index.tag;

import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.index.tag.TagIndex.TermIndexWriter;
import org.apache.lucene.store.IndexOutput;

public class TagWriter {
  private byte[] lastTermBytes = new byte[10];
  private int lastTermBytesLength = 0;
  private int lastFieldNumber = -1;
  IndexOutput output;

  void write(Term term, TagBlockBytes[] tagBlockBytes) {

  }
  
  void write(Term term, int[] blockNumbers, byte[][] blockBytes) throws IOException {
    
  }
  
  private void writeTerm(int fieldNumber, byte[] termBytes, int termBytesLength) throws IOException {
    int start = 0;
    final int limit = termBytesLength < lastTermBytesLength ? termBytesLength : lastTermBytesLength;
    while (start < limit) {
      if (termBytes[start] != lastTermBytes[start])
        break;
      start++;
    }
    final int length = termBytesLength - start;
    output.writeVInt(start); // write shared prefix length
    output.writeVInt(length); // write delta length
    output.writeBytes(termBytes, start, length); // write delta bytes
    output.writeVInt(fieldNumber); // write field num
    if (lastTermBytes.length < termBytesLength) {
      byte[] newArray = new byte[(int) (termBytesLength * 1.5)];
      System.arraycopy(lastTermBytes, 0, newArray, 0, start);
      lastTermBytes = newArray;
    }
    System.arraycopy(termBytes, start, lastTermBytes, start, length);
    lastTermBytesLength = termBytesLength;
  }
}
